//
//  ZYCMenuSliderView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/21.
//

#import "ZYCMenuSliderView.h"


@interface ZYCMenuSliderView ()
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIView *slider;
@property (nonatomic, strong) UIView *line;


@end
@implementation ZYCMenuSliderView

- (void)setLeftStr:(NSString *)leftStr{
    _leftStr = leftStr;
    [self.leftButton setTitle:leftStr forState:UIControlStateNormal];
}

- (void)setRightStr:(NSString *)rightStr{
    _rightStr = rightStr;
    [self.rightButton setTitle:rightStr forState:UIControlStateNormal];
}
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray <NSDictionary *>*)titles{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"FFFFFF");
        self.titles = titles;
        [self initUI];
    }
    return self;
}


- (void)initUI{
    for (NSInteger i = 0; i < self.titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000 + i;
        btn.titleLabel.font = kSystemFont(15.0f);
        NSDictionary *dic = self.titles[i];
        NSString *title =dic[@"title"];
        NSInteger count = [dic[@"count"] intValue];
        [btn setTitle:[NSString stringWithFormat:@"%@(%zd)",title,count] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (btn.tag == 1000) {
            [btn setTitleColor:HEX_RGB(@"#009B4C") forState:UIControlStateNormal];
            self.leftButton = btn;
        }else{
            [btn setTitleColor:HEX_RGB(@"#999999") forState:UIControlStateNormal];
            self.rightButton = btn;
        }
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width / 2, 46));
            make.left.equalTo(self).offset((i % self.titles.count) * self.bounds.size.width / 2);
        }];
    }
    
    [self addSubview:self.line];
    [self addSubview:self.slider];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(12.0f);
        make.bottom.equalTo(self).offset(-12.0f);
        make.width.mas_equalTo(1);
    }];
    
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.leftButton);
        make.bottom.equalTo(self.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(16, 4));
    }];
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EFEFEF");
    }
    return _line;
}

- (UIView *)slider {
    if (!_slider) {
        _slider = [[UIView alloc] init];
        _slider.backgroundColor = HEX_RGB(@"#009B4C");
        _slider.layer.cornerRadius = 2.0f;
        _slider.layer.masksToBounds = YES;
    }
    return _slider;
}

- (void)clickBtnAction:(UIButton *)sender{
    if (sender.tag == 1000) {//左边
        [self.leftButton setTitleColor:HEX_RGB(@"#009B4C") forState:UIControlStateNormal];
        [self.rightButton setTitleColor:HEX_RGB(@"#999999") forState:UIControlStateNormal];
        self.slider.transform = CGAffineTransformMakeTranslation(0, 0);
        if ([self.delegate respondsToSelector:@selector(menuSilderClickLeft)]) {
            [self.delegate menuSilderClickLeft];
        }
    }else{//右边
        [self.leftButton setTitleColor:HEX_RGB(@"999999") forState:UIControlStateNormal];
        [self.rightButton setTitleColor:HEX_RGB(@"#009B4C") forState:UIControlStateNormal];
        self.slider.transform = CGAffineTransformMakeTranslation(self.bounds.size.width / 2, 0);
        if ([self.delegate respondsToSelector:@selector(menuSilderClickRight)]) {
            [self.delegate menuSilderClickRight];
        }
    }
    

}


@end
